package controller;

import java.util.List;

import javax.persistence.EntityManagerFactory;

import org.eclipse.persistence.sessions.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import dao.UserDao;
import model.User;

@Controller
@RequestMapping("userController")
public class UserController {
	
	@Autowired
	UserDao userDao;
	
	@Autowired
	EntityManagerFactory entityManagerFactory;//依赖注入成功
	
	@RequestMapping("test1")//访问路径：http://localhost/SJTest/uc/test.do 
	public String test(){
		System.out.println("this is test enter...");
		List<User> list = entityManagerFactory.createEntityManager().createNamedQuery("User.findAll",User.class).getResultList();
		System.out.println(list);
		
		
		
		return "test";
		
	}
	
	

	@RequestMapping("test2")
	public String test2(){
		System.out.println("this is test2 enter...");
		User u = entityManagerFactory.createEntityManager().createNamedQuery("User.findById",User.class).setParameter("id", 5).getSingleResult();
		System.out.println(u);
		return "test";
	}
	
	
	//通过session操作
	@RequestMapping("test3")
	public String test3(){
		System.out.println("this is test3 enter...");
		Session session = entityManagerFactory.createEntityManager().unwrap(Session.class);
		List<User> o = (List<User>) session.executeQuery("User.findAll");
		System.out.println("session#:"+o);
		return "test";
	}
	
	//通过原生SQL操作
		@RequestMapping("test4")
		public String test4(){
			System.out.println("this is test4 enter...");
			User u= (User) userDao.getUserById(5);
			System.out.println("getUserById#:"+u);
			return "test";
		}
		
	
}
